package org.tigris.atlas.quizme.client.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.tigris.atlas.messages.Message;
import org.tigris.atlas.messages.MessageFactory;
import org.tigris.atlas.messages.MessageUtils;
import org.tigris.atlas.messages.Messages;
import org.tigris.atlas.quizme.client.form.RegistrationForm;
import org.tigris.atlas.quizme.client.utils.ClientUtils;
import org.tigris.atlas.quizme.client.utils.Constants;
import org.tigris.atlas.quizme.service.UserProfileServiceResponse;
import org.tigris.atlas.quizme.service.delegate.EntityMaintenanceServiceDelegate;
import org.tigris.atlas.quizme.service.delegate.ServiceDelegateFactory;
import org.tigris.atlas.quizme.transfer.TransferObjectFactory;
import org.tigris.atlas.quizme.transfer.UserProfile;

/**
 * Registers a user.
 * 
 * @author Robert Simmering
 */
public class RegisterAction  extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
		      HttpServletRequest request, HttpServletResponse response) {	
		RegistrationForm regForm = (RegistrationForm)form;
		
		if(!StringUtils.isBlank(regForm.getUserProfilePassword()) && !regForm.getUserProfilePassword().equals(regForm.getConfirmPassword())){
			Messages messages = MessageFactory.createMessages();
			Message msg = MessageUtils.createErrorMessage("errors.passwords.dont.match", null, null);
			messages.addMessage(msg);
			ClientUtils.getMessagesHandler().handleMessages( messages, regForm.getClass(), request);
			return mapping.findForward("fail");
		}
		// Need to convert the form object to a transfer object so that
		// the service layer can use it. Luckily this is generated into the form!
		UserProfile newUserProfile = TransferObjectFactory.createUserProfile();
		regForm.copyTo(newUserProfile);
		
		//use the Generated EntityMaintenanceService to save the TransferObject
		EntityMaintenanceServiceDelegate sd = ServiceDelegateFactory.createEntityMaintenanceServiceDelegate();
		UserProfileServiceResponse upResponse = sd.save(newUserProfile);
		if(upResponse.getMessages().hasErrorMessages()){
			ClientUtils.getMessagesHandler().handleMessages( upResponse.getMessages(), regForm.getClass(), request);
			return mapping.findForward("fail");
		}
		
		// Store the new user in the session
		UserProfile user = upResponse.getUserProfile();
	    request.getSession().setAttribute(Constants.USER_KEY, user);
	    
	    Message msg = MessageUtils.createInformationalMessage("success.register", null, null);
		Messages messages = MessageFactory.createMessages();
		messages.addMessage(msg);
		ClientUtils.getMessagesHandler().handleMessages(messages, regForm.getClass(), request);
		
		return mapping.findForward("success");
	}
}
